package com.quasar.hpatient.module.comm_web;

import android.content.Context;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;


import lib.quasar.base.frame.BasePresenter;
import lib.quasar.util.DimenUtil;
import lib.quasar.widget.web.CusWebTitle;
import lib.quasar.widget.web.CusWebView;
import lib.quasar.widget.web.WebManager;

/**
 * description: H5网页
 * created by kalu on 2017/5/25 13:46
 */
public class WebPresenter implements BasePresenter {

    private final String TAG = WebPresenter.class.getSimpleName();

    private CusWebView webView;

    void initWebClient(final Context context, RelativeLayout parent, final CusWebTitle title, final TextView close) {

        if (null == parent) return;

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.topMargin = DimenUtil.dp2px(45);

        final CusWebView webView = WebManager.getInstance();
        final ViewGroup parent1 = (ViewGroup) webView.getParent();
        if (null != parent1) {
            parent.removeAllViews();
        }

        webView.setLayoutParams(params);
        parent.addView(webView);

//        webView.setOnWebLoadChangeListener(new OnWebChangeListener() {
//            @Override
//            public void onWebLoadChange(int progress) {
//                LogUtil.e(TAG, "onWebLoadChange ==> progress = " + progress);
//                if (null != title) {
//                    title.drawLoad(progress);
//                }
//            }
//
//            @Override
//            public void onWebClickLink(String linkUrl) {
//                LogUtil.e(TAG, "onWebClickLink ==> linkUrl = " + linkUrl);
//            }
//
//            @Override
//            public void onWebPageChange(int pageCount) {
//                LogUtil.e(TAG, "onWebPageChange ==> pageCount = " + pageCount);
//                if (null != close) {
//                    close.setVisibility(pageCount > 1 ? View.VISIBLE : View.GONE);
//                }
//            }
//        });
    }

    void loadWebHtml(String htmlUrl) {

        if (null == webView || TextUtils.isEmpty(htmlUrl)) return;
        webView.loadUrl(htmlUrl);
    }

    boolean canGoBack() {

        if (null == webView) {
            return false;
        }

        if (webView.canGoBack()) {
            webView.goBack();
            return true;
        } else {
            return false;
        }
    }

    void backHtml(WebActivity activity) {

        if (null != webView) {

            if (webView.canGoBack()) {
                webView.goBack();
            } else {
                if (null != activity) {
                    activity.onBackPressed();
                }
            }
        }

        if (null != activity) {
            activity.onBackPressed();
        }
    }

    void pauseResource() {
        if (null == webView) return;
        webView.onPause();
        webView.pauseTimers(); //小心这个！！！暂停整个 WebView 所有布局、解析、JS。
    }

    void resumeResource() {
        if (null == webView) return;
        webView.onResume();
        webView.resumeTimers();
    }

    void destoryResource(RelativeLayout parent) {
        if (null != parent && null != webView) {
            webView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            webView.clearHistory();

            parent.removeView(webView);

            webView.stopLoading();
            webView.setWebChromeClient(null);
            webView.setWebViewClient(null);
            webView.destroy();
            webView = null;
        }
    }

    @Override
    public void recycler() {
    }
}
